<?php

/**
 * Class Bootstrap
 * @author: Lee Way
 * @copyright: PiuZiu
 */
class Bootstrap {

	/**
	 * Constructor
	 */
	function __construct() {
		// Get url
		$url = isset($_GET['url']) ? $_GET['url'] : null;
		$url = rtrim($url, '/');
		$url = explode('/', $_GET['url']);

		if (empty($url[0])) {
			require 'controllers/index.php';
			$controller = new Index();
			$controller->index();
			return false;
		}

		if (CommonUtils::endsWith($url[0], ".php")) {
			$file = 'controllers/' . $url[0];
			$url[0] = substr($url[0], 0, strlen($url[0]) - 4);
		} else {
			$file = 'controllers/' . $url[0] . '.php';
		}

		// check file exist
		if (file_exists($file)) {
			require $file;
		} else {
			// return error page if file not exist
			require 'controllers/error.php';
			$controller = new Error();
			$controller->index();
			return false;
		}

		// redirect to file
		$controller = new $url[0]();

		if (isset($url[1])) {
			// parameter url 1 solve
		}
		$controller->index();
	}
}

?>